global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"
set scheme plotplain
graph set window fontface "Times New Roman"

capture log close
log using "$log/6_reg_wage_growth_apr", replace

*Program to estimate regressions
cap program drop reg_est
program define reg_est 
args var name

	preserve 
	
	reghdfe `var' d_*, absorb(worker time) vce(cluster client_id)
	
	cap drop beta ub lb
	gen beta=.
	gen ub=.
	gen lb=.
	gen event_time=.
		
	local index=1
	forvalues i=-11/25 {
		replace event_time=`i'-8 if _n==`index'
		if `i'!=-1 {
			replace beta=_b[d_`index'] if _n==`index'
			replace ub=_b[d_`index'] + 1.96*_se[d_`index'] if _n==`index'
			replace lb=_b[d_`index'] - 1.96*_se[d_`index'] if _n==`index'	
		}
		if `i'==-1 {
			replace beta=0 if _n==`index'
			replace ub=0 if _n==`index'
			replace lb=0 if _n==`index'
		}
		local ++index
	}
	
	keep if beta!=.
	keep event_time beta ub lb	
	
	*Save estimates
	save "$output/`name'_estimates.dta", replace
	restore 
end 
	

*Program to estimate regressions
cap program drop reg_est_tab
program define reg_est_tab
args var name spec

	preserve 
	
	gen p1=time<=0 & treat==1
	gen p2=time>0 & time<=8 & treat==1
	gen p3=time>8 & treat==1
	gen slope1=time*p1
	gen slope2=time*p2 
	gen slope3=time*p3 
	
	*Variables to save estimates
		gen spec=""
		gen controls=""
		forvalues i=1/3 {
			gen beta`i'=.
			gen se`i'=.
		}
		gen n=.
		gen beta_change=.
		gen se_change=.
		
	*Regression
	reghdfe `var' p1 p2 p3 slope1 slope2 slope3, nocons absorb(worker time) vce(cluster client_id)

	replace spec="`spec'" if _n==1
	replace controls="Worker Time" if _n==1
	replace beta1=_b[slope1] if _n==1
	replace se1=_se[slope1] if _n==1
	replace beta2=_b[slope2] if _n==1
	replace se2=_se[slope2] if _n==1
	replace beta3=_b[slope3] if _n==1
	replace se3=_se[slope3] if _n==1
	replace n=`e(N)' if _n==1
	
	nlcom _b[slope3]-_b[slope1]
	replace beta_change=r(b)[1,1] if _n==1
	replace se_change=sqrt(r(V)[1,1]) if _n==1

	*Regression within firm
	reghdfe `var' p1 p2 p3 slope1 slope2 slope3, nocons absorb(worker client_id##time) vce(cluster client_id)

	replace spec="`spec'" if _n==2
	replace controls="Worker Firm-Time" if _n==2
	replace beta1=_b[slope1] if _n==2
	replace se1=_se[slope1] if _n==2
	replace beta2=_b[slope2] if _n==2
	replace se2=_se[slope2] if _n==2
	replace beta3=_b[slope3] if _n==2
	replace se3=_se[slope3] if _n==2
	replace n=`e(N)' if _n==2
	
	nlcom _b[slope3]-_b[slope1]
	replace beta_change=r(b)[1,1] if _n==2
	replace se_change=sqrt(r(V)[1,1]) if _n==2
	
	keep if _n<=2
	keep spec controls beta* se* n
	
	*Save estimates
	save "$output/`name'_table_estimates.dta", replace
	
	restore 
end 	

*********************************************************
*Fig 5a: Raw Averages
*********************************************************
	use yr_month client_id state emp_pur_c salaried base ot retire health base0 salaried0 using "$output\stayers_panel_treat.dta", clear
	
	*Keep main sample
	keep if base0>=700 & base0<993 & salaried0==1
	gen treat=base0>=700 & base0<=913
	gen log_base=log(base)
	gen log_ot=log(ot)

	*Collapse and plot 
	preserve 
		collapse base log_base, by(treat yr_month)
		gen time=mofd(yr_month)-mofd(mdy(4,01,2016))
		
		label define time_lab -12 "Apr 2015" -11 "May 2015" -10 "Jun 2015" ///
		-9 "Jul 2015" -8 "Aug 2015" -7 "Sep 2015" -6 "Oct 2015" -5 "Nov 2015" -4 "Dec 2015" ///
		-3 "Jan 2016" -2 "Feb 2016" -1 "Mar 2016" 0 "Apr 2016" 1 "May 2016" 2 "Jun 2016" ///
		3 "Jul 2016" 4 "Aug 2016" 5 "Sep 2016" 6 "Oct 2016" 7 "Nov 2016" 8 "Dec 2016" ///
		9 "Jan 2017" 10 "Feb 2017" 11 "Mar 2017" 12 "Apr 2017" 13 "May 2017" 14 "Jun 2017" ///
		15 "Jul 2017" 16 "Aug 2017" 17 "Sep 2017" 18 "Oct 2017" 19 "Nov 2017" 20 "Dec 2017" ///
		21 "Jan 2018" 22 "Feb 2018" 23 "Mar 2018" 24 "Apr 2018" 25 "May 2018", replace
		label values time time_lab 
		
		graph twoway (connect base time if treat==1, lc(black) mcolor(black)) ///
			(connect base time if treat==0, lc(maroon) mcolor(maroon) msymbol(D)), ///
			xline(1 8, lcolor(black)) xtitle("Month") xlabel(-12(3)25, valuelabel angle(45)) ///
			ytitle("Average Weekly Base Pay")  ylabel(750(50)1050) ///
			legend(subtitle("Base Pay in Apr 2016") order(1 "[700,913]" 2 "[913,993)") ring(0) position(5) col(2))
		graph export "$graphs/fig5a_base_pay_raw_apr.eps", replace
		graph export "$graphs/fig5a_base_pay_raw_apr.jpg", replace
		
		graph twoway (connect log_base time if treat==1, lc(black) mcolor(black)) ///
			(connect log_base time if treat==0, lc(maroon) mcolor(maroon) msymbol(D)), ///
			xline(1 8, lcolor(black)) xtitle("Month") xlabel(-12(3)25, valuelabel angle(45)) ///
			ytitle("Average Log Weekly Base Pay")   ///
			legend(subtitle("Base Pay in Apr 2016") order(1 "[700,913]" 2 "[913,993)") ring(0) position(5) col(2))
		graph export "$graphs/fig5a_log_base_pay_raw_apr.eps", replace
		graph export "$graphs/fig5a_log_base_pay_raw_apr.jpg", replace
	restore 

*********************************************************
*Fig 5b & Table 1 C1-C2 (Control is 953-993 in Dec 2016)
*********************************************************
	gen time=mofd(yr_month)-mofd(mdy(4,01,2016))
	local index=1
	forvalues i=-11/25 {
		gen d_`index'= treat==1 & time==`i'
		label var d_`index' "Treat=1 and Event Time=`i'"
		local ++index
	}
	drop d_11
	egen worker=group(emp_pur_c state client_id)
	
	reg_est base fig5b_base_apr
	reg_est_tab base fig5b_base_apr c953
	
	reg_est ot fig5b_ot_apr
	reg_est_tab ot fig5b_ot_apr c953
	
	reg_est log_base fig5b_log_base_apr
	reg_est_tab log_base fig5b_log_base_apr c953
	
	reg_est log_ot fig5b_log_ot_apr
	reg_est_tab log_ot fig5b_log_ot_apr c953
	
	reg_est retire fig5b_retire_apr
	reg_est_tab retire fig5b_retire_apr c953
	
	reg_est health fig5b_health_apr
	reg_est_tab health fig5b_health_apr c953
	
	drop worker 
	keep if treat==1 
	save "$temp/temp_treat2016_apr.dta", replace
	
*********************************************************
*Fig 5c & Table 1 C3-C4 (Control is 700-913 in Apr 2014)
*********************************************************
	use yr_month client_id state emp_pur_c salaried base ot retire health salaried0 base0 using "$output\stayers_panel_control.dta", clear
	
	*Keep main sample
	keep if base0>=700 & base0<993 & salaried0==1
	gen treat=base0>=700 & base0<=913
	gen log_base=log(base)
	gen log_ot=log(ot)
	
	*Define regressors
	gen time=mofd(yr_month)-mofd(mdy(4,01,2014))
	local index=1
	forvalues i=-11/25 {
		gen d_`index'= treat==1 & time==`i'
		label var d_`index' "Treat=1 and Event Time=`i'"
		local ++index
	}
	drop d_11

	*Define control group
	keep if treat==1
	replace treat=0
	forvalues i=1/37 {
		capture replace d_`i'=0
	}
	append using "$temp/temp_treat2016_apr.dta"
	egen worker=group(emp_pur_c state client_id treat)
	
	reg_est base fig5c_base_apr
	reg_est_tab base fig5c_base_apr c953
	
	reg_est ot fig5c_ot_apr
	reg_est_tab ot fig5c_ot_apr c953
	
	reg_est log_base fig5c_log_base_apr
	reg_est_tab log_base fig5c_log_base_apr c953
	
	reg_est log_ot fig5c_log_ot_apr
	reg_est_tab log_ot fig5c_log_ot_apr c953
	
	reg_est retire fig5c_retire_apr
	reg_est_tab retire fig5c_retire_apr c953
	
	reg_est health fig5c_health_apr
	reg_est_tab health fig5c_health_apr c953
	
log close

